package com.tycmc.signelectroniccontracts.logic.htong;

import com.tycmc.signelectroniccontracts.act.htong.ActHtList;
import com.tycmc.signelectroniccontracts.entity.HtEntity;
import com.tycmc.signelectroniccontracts.tools.base.LogicParent;
import com.tycmc.signelectroniccontracts.tools.net.NetUrl;
import com.tycmc.signelectroniccontracts.tools.net.NetWorkUtils;
import com.tycmc.signelectroniccontracts.tools.net.RequestAction;
import com.tycmc.signelectroniccontracts.tools.net.result.NetworkResult;
import com.tycmc.signelectroniccontracts.tools.other.TextShow;
import com.tycmc.signelectroniccontracts.tools.show.ProDialog;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PreHtList extends LogicParent {

    private ActHtList context;
    private HtListInterface pre;

    public PreHtList(HtListInterface pre) {
        this.pre = pre;
        context = ((ActHtList) pre);
    }

    public void postData(String type) {
        RequestAction action = new RequestAction(NetUrl.queryContractSignList);
        action.putBody("signStatusID", type);
        final ProDialog dialog = new ProDialog(context, "");
        dialog.show();
        new NetWorkUtils(context).postJson(action, new NetworkResult() {
            @Override
            public void sucess(Map<String, Object> data, String str) {
                dialog.dismiss();
                List<HtEntity> list = new ArrayList<>();
                if (data.get("contractList") instanceof List) {
                    List<Map> mapList = (List<Map>) data.get("contractList");
                    for (int i = 0; i < mapList.size(); i++) {
                        Map map = mapList.get(i);
                        HtEntity entity = new HtEntity();
                        entity.setArea(TextShow.valueOf(map.get("area")));
                        entity.setCmpSignStatus(TextShow.valueOf(map.get("cmpSignStatus")));
                        entity.setClientName(TextShow.valueOf(map.get("clientName")));
                        entity.setContractNo(TextShow.valueOf(map.get("contractNo")));
                        entity.setContractType(TextShow.valueOf(map.get("contractType")));
                        entity.setClientSignStatusID(TextShow.valueOf(map.get("clientSignStatusID")));
                        entity.setSignStatus(TextShow.valueOf(map.get("signStatus")));
                        entity.setSignStatusID(TextShow.valueOf(map.get("signStatusID")));
                        entity.setCmpName(TextShow.valueOf(map.get("cmpName")));
                        entity.setClientSignStatus(TextShow.valueOf(map.get("clientSignStatus")));
                        entity.setSignDate(TextShow.valueOf(map.get("signDate")));
                        entity.setEquNo(TextShow.valueOf(map.get("equNo")));
                        entity.setContractAuditDate(TextShow.valueOf(map.get("contractAuditDate")));
                        entity.setClientManager(TextShow.valueOf(map.get("clientManager")));
                        entity.setSignTypeID(TextShow.valueOf(map.get("signTypeID")));
                        List<HtEntity.SubContractListEntity> entityList = new ArrayList<>();
                        if (map.get("subContractList") instanceof List) {
                            List<Map> subContractList = (List<Map>) map.get("subContractList");
                            for (int j = 0; j < subContractList.size(); j++) {
                                Map map1 = subContractList.get(j);
                                HtEntity.SubContractListEntity listEntity = new HtEntity.SubContractListEntity();
                                listEntity.setCmpSignStatus(TextShow.valueOf(map1.get("cmpSignStatus")));
                                listEntity.setCopyNum(TextShow.valueOf(map1.get("copyNum")));
                                listEntity.setSignProperty(TextShow.valueOf(map1.get("signProperty")));
                                listEntity.setClientSignStatusID(TextShow.valueOf(map1.get("clientSignStatusID")));
                                listEntity.setSignStatus(TextShow.valueOf(map1.get("signStatus")));
                                listEntity.setSignStatusID(TextShow.valueOf(map1.get("signStatusID")));
                                listEntity.setClientSignStatus(TextShow.valueOf(map1.get("clientSignStatus")));
                                listEntity.setSubContractNo(TextShow.valueOf(map1.get("subContractNo")));
                                listEntity.setSignDate(TextShow.valueOf(map1.get("signDate")));
                                listEntity.setContractFileStatusID(TextShow.valueOf(map1.get("contractFileStatusID")));
                                listEntity.setContractFileStatus(TextShow.valueOf(map1.get("contractFileStatus")));
                                listEntity.setCmpSignStatusID(TextShow.valueOf(map1.get("cmpSignStatusID")));
                                listEntity.setContractName(TextShow.valueOf(map1.get("contractName")));
                                listEntity.setPdfReadState(TextShow.valueOf(map1.get("pdfReadState")));
                                entityList.add(listEntity);
                            }
                        }
                        entity.setSubList(entityList);

                        list.add(entity);
                    }
                }
                pre.addAll(list,false);
            }

            @Override
            public void failed(Object data) {
                pre.addAll(null,false);
                dialog.dismiss();
            }
        });
    }

    public interface HtListInterface {
        void addAll(List<HtEntity> list,boolean isAdd);
    }
}
